package service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.sun.org.apache.bcel.internal.generic.NEW;
import entity.ClassroomTable;
import entity.CurriculumList;
import entity.TeacherTable;
import entity.exam;
import mapper.ClassroomTableDao;
import mapper.ExamDao;
import mapper.TeacherTableDao;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import service.ExamService;
import utils.PageObject;

import javax.annotation.Resource;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.time.Period;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.SimpleTimeZone;

@Service("ExamService")
public class ExamServiceImpl implements ExamService {


    @Resource
    private ClassroomTableDao classroomTableDao;

    @Resource
    private TeacherTableDao teacherTableDao;

    @Resource
    private ExamDao examDaoDao;
    @Override
    public List<exam> queryClassnameAndCourse() {
        List<exam> exams = examDaoDao.queryClassnameAndCourse();
        return  exams;
    }

    @Override
    public List<exam> autoFind(Date date1, Date date2, String time) {
        String[] split = time.split("-");
        String  tempDate_first= date1.getYear()+1900+"-"
                +(date1.getMonth()+1)+"-"
                +date1.getDate()+" "
                +date1.getHours()+":"+date1.getMinutes()+":"+date1.getSeconds();
        String tempDate_last= date2.getYear()+1900+"-"
                +(date2.getMonth()+1)+"-"
                +date2.getDate()+" "
                +date2.getHours()+":"+date2.getMinutes()+":"+date2.getSeconds();
        List<exam> exams = examDaoDao.autoFind(tempDate_first, tempDate_last, split[0], split[1]);
        return exams;
    }

    @Override
    public List<exam> cd() {
        List<exam> cd = examDaoDao.cd();
        return cd;
    }

    @Override
    public List<exam> findByClassroomAndTeacherAndClassnameAndCourse(Date tempDate_first1,Date tempDate_last1,String teacher, String classname,String classroomname,String course) {
        //时间
        String tempTime_first=null;
        String tempTime_last=null;

        //日期
        String tempDate_first=null;
        String tempDate_last=null;

        if(tempDate_first1!=null){
            tempTime_first=tempDate_first1.getHours()+":"+tempDate_first1.getMinutes()+":"+tempDate_first1.getSeconds();
            tempDate_first=tempDate_first1.getYear()+1900+"-"+(tempDate_first1.getMonth()+1)+"-"+tempDate_first1.getDate();
            tempDate_first+=" "+tempTime_first;
        }
        if (tempDate_last1!=null) {
            tempTime_last=tempDate_last1.getHours()+":"+tempDate_last1.getMinutes()+":"+tempDate_last1.getSeconds();
            tempDate_last=tempDate_last1.getYear()+1900+"-"+(tempDate_last1.getMonth()+1)+"-"+tempDate_last1.getDate();
            tempDate_last+=" "+tempTime_last;
        }
        List<exam> byClassroomAndTeacherAndClassnameAndCourse = examDaoDao.findByClassroomAndTeacherAndClassnameAndCourse(tempDate_first,tempDate_last,teacher, classname, classroomname, course);
        return  byClassroomAndTeacherAndClassnameAndCourse;
    }

    @Override
    public CurriculumList queryById(Integer pkId) {
        return null;
    }

    @Override
    public PageObject queryAllByLimit(JSONObject object) {
        //当前第几页
        Integer page = object.getInteger("page");
        //每页几条
        Integer limit = object.getInteger("limit");
        //计算开始位置
        Integer startindex = (page - 1) * limit;
        List<exam> exams = examDaoDao.queryAllByLimit(startindex, limit);

        Integer selectcount = examDaoDao.selectcount();
        PageObject pageObject = new PageObject();
        pageObject.setPageCount(page);//第几页
        pageObject.setPageSize(limit);//每页几条
        pageObject.setRowCount(selectcount);//总条数
        pageObject.setPageCount(selectcount / limit);//总页数
        pageObject.setRecords(Collections.singletonList(exams));//当前需展示数据
        return pageObject;
    }

    @Override
    public JSONObject insert(JSONObject insertobj) throws Exception {
        JSONObject result = new JSONObject();
        //考室
        Integer classname = null;
        ClassroomTable classnameObj = classroomTableDao.queryByClassname(insertobj.getString("教室名称"));
        if(classnameObj!=null){
            classname =classnameObj.getPkId();
        }
        classnameObj =new ClassroomTable();
        classnameObj.setClassroomname(insertobj.getString("教室名称"));
        classnameObj.setKszw(insertobj.getString("人数"));
        classnameObj.setXq(insertobj.getString("校区"));
        try {

            if (classname==null){
                classroomTableDao.insert(classnameObj);
                classname=classroomTableDao.queryByClassname(insertobj.getString("教室名称")).getPkId();
            }else{
                classroomTableDao.update(classnameObj);
            }
        }catch (Exception E){
            result.put("code", "-1");
            result.put("msg", "插入数据失败,请核对");
            return result;
        }
        //监考人
        TeacherTable teacherTable = new TeacherTable();
        teacherTable.setMainteacher(insertobj.getString("主监考教师"));
        teacherTable.setViceteacher(insertobj.getString("副监考教师"));
        teacherTable.setXunkaoteacher(insertobj.getString("巡考教师"));
        teacherTable.setIscancelinvigilate(insertobj.getString("是否免监考"));
        teacherTable.setMaxinvigilatetimes("1");
        TeacherTable teacherTable1 = teacherTableDao.queryByOR(teacherTable);
        try {
            if(teacherTable1==null){
                teacherTableDao.insert(teacherTable);
                teacherTable1=teacherTableDao.queryByOR(teacherTable);
            }else{
                teacherTableDao.update(teacherTable);
            }
        }catch (Exception E){
            result.put("code", "-1");
            result.put("msg", "插入数据失败,请核对");
            return result;
        }

        //考试信息
        exam exam = new exam();
        String date=new String();
        String tiem=new String();
        String dateTemp = insertobj.getString("日期");
        String tiemTemp = insertobj.getString("考试时间");
        Integer  i = dateTemp.indexOf('.');
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yy");
        if (i==-1){
            date=simpleDateFormat.parse(dateTemp).getYear()+1900+"/"+(simpleDateFormat.parse(dateTemp).getMonth()+1)+"/"+(simpleDateFormat.parse(dateTemp).getDate());
        }else{
            date=dateTemp;
        }
        simpleDateFormat.applyPattern("kk:mm:ss");
        tiem = simpleDateFormat.parse(tiemTemp).getHours() + ":" + simpleDateFormat.parse(tiemTemp).getMinutes() + ":" + simpleDateFormat.parse(tiemTemp).getSeconds();

        exam.setDate(date);
        exam.setTime(tiem);
        exam.setDate_time(date+" "+tiem);
        exam.setAmorpm(insertobj.getString("上午/下午"));
        exam.setMajor(insertobj.getString("专业"));
        exam.setYear(insertobj.getString("年级"));
        exam.setClassname(insertobj.getString("班级名称"));
        exam.setCourse(insertobj.getString("课程名称"));
        exam.setTeachers(teacherTable1.getPkId());
        exam.setClassroom(classname);
        try {
            examDaoDao.insert(exam);
        }catch (Exception E){
            result.put("code", "-1");
            result.put("msg", "插入数据失败,请核对");
            return result;
        }
        result.put("code", "0");
        result.put("msg", "数据插入成功");
        return result;
    }

    @Override
    public JSONObject update(JSONObject updateobj) {
        return null;
    }

    @Override
    public boolean deleteById(Integer pkId) {
        return false;
    }

    @Override
    public PageObject selectAll(JSONObject object) {
        return null;
    }

    @Override
    public JSONObject excelimport(MultipartFile multipartFile) throws Exception {
        return null;
    }
}
